/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import functions.Dogs;
import functions.HealingSet;
import functions.Dogy;
import stuff.HealingSetComparator;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import functions.ItemList;
import java.util.Calendar;
import stuff.FDate;

/**
 *
 * @author JArik
 */
public class Dog {

    private int id;
    private int userId;
    private String breed;
    private int age;
    private String nick;
    private Date birthDate;
    private Date deathDate;



    public Dog(int newId, Date bd, int nUserId, String newBreed, String nNick) {
        id = newId;
        userId = nUserId;
        breed = newBreed;
        birthDate = bd;
        nick = nNick;
    }
    public Dog(int newId, Date bd, Date deathD, int nUserId, String newBreed, String nNick) {
        id = newId;
        userId = nUserId;
        breed = newBreed;
        birthDate = bd;
        deathDate =deathD;
        nick = nNick;
    }
    public String getAge() {
        if(birthDate != null)
        {
            long time =0;
            if(!isDead())
               time = Calendar.getInstance().getTime().getTime() - birthDate.getTime();
            else
               time = deathDate.getTime() - birthDate.getTime();
            double days = time/(1000*60*60*24);
            return (int)(days/365) + "."+ Math.round((days/365 - (int)(days/365))*12);
        }
        else
            return "0.00";
    }

    public boolean isDead()
    {
        return deathDate != null;
    }
    public Date getDeathDate() {
        if(deathDate == null) return null;
        return new FDate(deathDate.getTime());
    }

    public void setDeathDate(Date deathDate) {
        this.deathDate = deathDate;
    }
    
    public String getBreed() {
        return breed;
    }

    public String getNick() {
        return nick;
    }

    public ArrayList<HealingSet> getHealing() throws SQLException {

        return Dogs.getHealing(id);
    }

    public void setBreed(String text) {
        breed =text;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setNick(String text) {
        nick = text;
    }
    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    private <T extends Item> ArrayList<ItemList<T>> groupByDate(ArrayList<T> list) {
        ArrayList<ItemList<T>> allVisits = new ArrayList<ItemList<T>>();

        if (list.size() > 0 && list.get(0) != null) {
            Date date = list.get(0).getDisDate();
            ItemList<T> diagDates = new ItemList<T>(date);
            diagDates.add(list.get(0));

            for (int i = 1; i < list.size(); i++) {
                if (date.equals(list.get(i).getDisDate())) {
                    diagDates.add(list.get(i));
                } else {
                    allVisits.add(diagDates); // add to array of arrays
                    diagDates = new ItemList<T>(list.get(i).getDisDate());
                    diagDates.add(list.get(i));
                    date = list.get(i).getDisDate();
                }
            }
            allVisits.add(diagDates);
        }
        return allVisits;
    }

    public int getId() {
        return id;
    }

    public void setBirthDate(Date dt)
    {
        birthDate = dt;
    }
    public Date getBirthDate()
    {
        if(birthDate == null) return null;
        return new FDate(birthDate.getTime());
    }
    public Date getBirthDateSQL()
    {
        if(birthDate == null) return null;
        return new Date(birthDate.getTime());
    }
    public String toString() {
        double age;
        try
        {
          age = Double.parseDouble(getAge());
        } catch(Exception e)
        {   age = 0;    }
        String let = "";

        if(age == 0)
            let = " лет";
        if(age == 1)
            let = " год";
        if(age > 1 && age < 5)
            let = " года";
        else
            let = " лет";
        
        if(nick !=null)
            if(breed != null)
               return breed + "(" + nick + ")       \t" + printDeathDate() + getAge() + let;
            else
               return "Без породы "+"("+nick+")      \t" + printDeathDate() + getAge() + let;
        else
           return breed +"      \t"+ printDeathDate() + getAge() + let;
    }

    public String printDeathDate()
    {
        if(isDead())
            return "† "+getDeathDate()+ "  ";
        else return "";
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Dog) {
            Dog toComp = (Dog) obj;
            if (toComp.getId() == id) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 53 * hash + this.id;
        return hash;
    }
}
